package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.ItemBank;
import com.example.demo.entity.QuestionCenter;
import com.example.demo.mapper.QuestionCenterMapper;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/***
 ** Create by Wedream on 2021/7/28
 */
@RestController
@RequestMapping("/questionCenter")
public class QuestionCenterController {
    
    @Resource
    QuestionCenterMapper questionCenterMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody QuestionCenter questionCenter) {
        questionCenterMapper.insert(questionCenter);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        questionCenterMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<QuestionCenter> wrapper = Wrappers.<QuestionCenter>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
           wrapper.like(QuestionCenter::getQuestion,search);
        }
        Page<QuestionCenter> questionCenterPage = questionCenterMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(questionCenterPage);
    }

    // 导出Excel
    @ResponseExcel(name = "questionCenter", sheet = "questionCenter")
    @GetMapping("/downloadExcel")
    public List<QuestionCenter> DownloadExcel(@RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "1000") Integer pageSize,
                                        @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<QuestionCenter> wrapper = Wrappers.<QuestionCenter>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(QuestionCenter::getQuestion,search);
        }
        Page<QuestionCenter> questionCenterPage = questionCenterMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        List<QuestionCenter> records = questionCenterPage.getRecords();
        return records;
    }

    @PutMapping //改
    public Result<?> update(@RequestBody QuestionCenter questionCenter) {
        questionCenterMapper.updateById(questionCenter);
        return Result.success();
    }
}
